<?php
/**
 * 报表管理
 * @author jianhui
 *
 */
class Report extends MY_Controller{
    var $reportType;
    function Report(){
        parent::MY_Controller();
        $this->fundList = array(1=>'专营店营销基金', 2=>'区域营销基金', 3 => '车展基金', 4=>'巡展基金', 5=>'专项基金');
        $this->reportList = array(
            1 => array(1=>'项目费用总表', 2=>'线上广宣传播明细表', 3=>'线下营销活动明细表'),
            2 => array(1=>'项目费用总表', 2=>'线上广宣传播明细表', 3=>'线下营销活动明细表'),
            3 => array(1=>'项目费用总表', 2=>'项目费用明细表'),
            4 => array(1=>'项目费用总表', 2=>'项目费用明细表'),
            5 => array(1=>'项目费用总表', 2=>'线上广宣传播明细表', 3=>'线下营销活动明细表'),
        );
        $this->load->model('timeUnitModel');
    }
    /**
     * 载入报表类型json数据
     */
    function loadReportList(){
        $fundType = (int)$this->input->post('fundType');
        require_once APPPATH.'libraries/JSON.php';
        $list = array();
        $list[] = array('key'=>'','value'=>'请选择');
        if (isset($this->reportList[$fundType])){
            foreach ($this->reportList[$fundType] as $k => $v){
                $list[] = array('key'=>$k, 'value'=>$v);
            }
        }
        echo jsonEncode($list);
        exit();
    }
    /**
     * 报表管理首页
     */
    function index(){
        //查询条件
        //专营店筛选
        $area = $this->input->get('area');
        $this->viewData['area'] = $area;
        $region = $this->input->get('region');
        $this->viewData['region'] = $region;
        $spsarea = $this->input->get('spsarea');
        $this->viewData['spsarea'] = $spsarea;
        $province = $this->input->get('province');
        $this->viewData['province'] = $province;
        $city = $this->input->get('city');
        $this->viewData['city'] = $city;
        $storeId = $this->input->get('storeId');
        $this->viewData['storeId'] = $storeId;
        $keyword = $this->input->get('keyword');
        $this->viewData['keyword'] = $keyword;
        //年份
        $year = $this->input->get('year');
        $year || $year = $this->config->item('nowYear');
        $this->viewData['year'] = $year;
        //时间单位
        $timeUnit = (int)$this->input->get('timeUnit');
        $timeUnit || $timeUnit = $this->timeUnitModel->getNowTimeUnit();
        $this->viewData['timeUnit'] = $timeUnit;
        //是否为补报
        $isBubao = (int)$this->input->get('isBubao');
        //基金类型
        $fundType = (int)$this->input->get('fundType');
        //报表类型
        $reportType = (int)$this->input->get('reportType');
        if (isset($_GET['submit']) && $_GET['submit'] == 'export'){
            isset($this->reportList[$fundType][$reportType]) || showError('请选择正确的报表类型');
            //条件参数
            $params = array('area'=>$area, 'region'=>$region,'spsarea'=>$spsarea, 'province'=>$province, 'city'=>$city, 'storeId'=>$storeId, 'keyword'=>$keyword, 'year'=>$year, 'timeUnit'=>$timeUnit, 'isBubao'=>$isBubao);
            //导出文件名
            $fileName = $this->fundList[$fundType].'-'.$this->reportList[$fundType][$reportType].'-'.$year.'年';
            if ($timeUnit){
                $timeUnitList = $this->timeUnitModel->getCacheData('timeUnitList');
                isset($timeUnitList[$year][$timeUnit]) && $fileName .= '-'.$timeUnitList[$year][$timeUnit];
            }
            //设置excel文件下载头
            setExcelDownHeader($fileName);
            switch ($fundType){
                case 1://专营店营销基金
                    $this->getCommonExcel($reportType, $params);
                    break;
                case 2://区域营销基金
                    $this->getAreaExcel($reportType, $params);
                    break;
                case 3://车展基金
                    $this->getCarExcel($reportType, $params);
                    break;
                case 4://巡展基金
                    $this->getXunExcel($reportType, $params);
                    break;
                case 5://专项基金
                    $this->getSpecialExcel($reportType, $params);
                    break;
            }
        }else{
            //面包屑
            $this->viewData['breadcrumb'][] = array('url'=>printUrl('Report', 'index'),'title'=>'报表管理');
            
            $this->load->view('Report/index', $this->viewData);
        }
    }
    /**
     * 专营店营销基金报表初始化
     * @param number $reportType
     * @param array $params
     */
    function getCommonExcel($reportType, $params){
        //主线流程条件
        $procWhere = array();
        //查找对应地区列表
        $placeStoreList = $this->sellpointModel->searchStore($params['area'], $params['region'], '', $params['province'], $params['city'], $params['storeId'], $params['keyword']);
        $placeStoreIds = array_keys($placeStoreList);
        $this->viewData['allStoreList'] = $placeStoreList;
        //权限内专营店列表
        $viewStoreIds = $this->userLib->getViewStoreIds();
        if ($viewStoreIds == '*'){
            $procWhere['storeId'] = array('in', $placeStoreIds);
        }else{
            $procWhere['storeId'] = array('in', array_intersect($viewStoreIds, $placeStoreIds));
        }
        $procWhere['year'] = $params['year'];
        $procWhere['timeUnit'] = $params['timeUnit'];
        $this->load->model('commonProcessModel');
        $procList = $this->commonProcessModel->getNewList($procWhere);
        $this->viewData['procList'] = $procList;
        //主线流程ID集
        $procIds = array_keys($procList);
        $this->viewData['procIds'] = $procIds;
        switch ($reportType){
            case 1:
                $this->getCommonProjectExcel($params);
                break;
            case 2:
                $this->getCommonOnlineExcel($params);
                break;
            case 3:
                $this->getCommonOfflineExcel($params);
                break;
        }
    }
    /**
     * 专营店营销基金-项目费用总表
     * @param array $params
     */
    function getCommonProjectExcel($params){
        $this->load->view('Report/commonProjectExcel', $this->viewData);
    }
    /**
     * 专营店营销基金-线上广宣传播明细表
     * @param array $params
     */
    function getCommonOnlineExcel($params){
        //线上广宣传播明细
        $this->load->model('commonOnlineModel');
        $onlineWhere = array('procId' => array('in', $this->viewData['procIds']));
        $onlineList = $this->commonOnlineModel->getNewList($onlineWhere, NULL, NULL, '', 'procId ASC');
        $this->viewData['onlineList'] = $onlineList;
        
        $this->load->view('Report/commonOnlineExcel', $this->viewData);
    }
    /**
     * 专营店营销基金-线下营销活动明细表
     * @param array $params
     */
    function getCommonOfflineExcel($params){
        //线下营销活动明细
        $this->load->model('commonOfflineModel');
        $offlineWhere = array('procId' => array('in', $this->viewData['procIds']));
        $offlineList = $this->commonOfflineModel->getNewList($offlineWhere, NULL, NULL, '', 'procId ASC');
        $this->viewData['offlineList'] = $offlineList;
        
        $this->load->view('Report/commonOfflineExcel', $this->viewData);
    }
    /**
     * 区域营销基金报表初始化
     * @param number $reportType
     * @param array $params
     */
    function getAreaExcel($reportType, $params){
        //主线流程条件
        $procWhere = array();
        //查找对应地区列表
        $placeStoreList = $this->sellpointModel->searchStore($params['area'], $params['region'], '', $params['province'], $params['city'], $params['storeId'], $params['keyword']);
        $placeStoreIds = array_keys($placeStoreList);
        $this->viewData['allStoreList'] = $placeStoreList;
        //权限内专营店列表
        $viewStoreIds = $this->userLib->getViewStoreIds();
        if ($viewStoreIds == '*'){
            $procWhere['storeId'] = array('in', $placeStoreIds);
        }else{
            $procWhere['storeId'] = array('in', array_intersect($viewStoreIds, $placeStoreIds));
        }
        $procWhere['year'] = $params['year'];
        $procWhere['timeUnit'] = $params['timeUnit'];
        $this->load->model('areaProcessModel');
        $procList = $this->areaProcessModel->getNewList($procWhere);
        $this->viewData['procList'] = $procList;
        //主线流程ID集
        $procIds = array_keys($procList);
        $this->viewData['procIds'] = $procIds;
        switch ($reportType){
            case 1:
                $this->getAreaProjectExcel($params);
                break;
            case 2:
                $this->getAreaOnlineExcel($params);
                break;
            case 3:
                $this->getAreaOfflineExcel($params);
                break;
        }
    }
    /**
     * 区域营销基金-项目费用总表
     * @param array $params
     */
    function getAreaProjectExcel($params){
        $this->load->view('Report/areaProjectExcel', $this->viewData);
    }
    /**
     * 区域营销基金-线上广宣传播明细表
     * @param array $params
     */
    function getAreaOnlineExcel($params){
        //线上广宣传播明细
        $this->load->model('areaOnlineModel');
        $onlineWhere = array('procId' => array('in', $this->viewData['procIds']));
        $onlineList = $this->areaOnlineModel->getNewList($onlineWhere, NULL, NULL, '', 'procId ASC');
        $this->viewData['onlineList'] = $onlineList;
    
        $this->load->view('Report/areaOnlineExcel', $this->viewData);
    }
    /**
     * 区域营销基金-线下营销活动明细表
     * @param array $params
     */
    function getAreaOfflineExcel($params){
        //线下营销活动明细
        $this->load->model('areaOfflineModel');
        $offlineWhere = array('procId' => array('in', $this->viewData['procIds']));
        $offlineList = $this->areaOfflineModel->getNewList($offlineWhere, NULL, NULL, '', 'procId ASC');
        $this->viewData['offlineList'] = $offlineList;
    
        $this->load->view('Report/areaOfflineExcel', $this->viewData);
    }
    /**
     * 车展基金报表初始化
     * @param number $reportType
     * @param array $params
     */
    function getCarExcel($reportType, $params){
        //主线流程条件
        $procWhere = array();
        //查找对应地区列表
        $placeStoreList = $this->sellpointModel->searchStore($params['area'], $params['region'], '', $params['province'], $params['city'], $params['storeId'], $params['keyword']);
        $placeStoreIds = array_keys($placeStoreList);
        $this->viewData['allStoreList'] = $placeStoreList;
        //权限内专营店列表
        $viewStoreIds = $this->userLib->getViewStoreIds();
        if ($viewStoreIds == '*'){
            $procWhere['storeId'] = array('in', $placeStoreIds);
        }else{
            $procWhere['storeId'] = array('in', array_intersect($viewStoreIds, $placeStoreIds));
        }
        $procWhere['year'] = $params['year'];
        $procWhere['timeUnit'] = $params['timeUnit'];
        $this->load->model('carProcessModel');
        $procList = $this->carProcessModel->getNewList($procWhere);
        $this->viewData['procList'] = $procList;
        //主线流程ID集
        $procIds = array_keys($procList);
        $this->viewData['procIds'] = $procIds;
        switch ($reportType){
            case 1:
                $this->getCarProjectExcel($params);
                break;
            case 2:
                $this->getCarDetailExcel($params);
                break;
        }
    }
    /**
     * 车展基金报表-项目费用总表
     * @param array $params
     */
    function getCarProjectExcel($params){
        $this->load->view('Report/carProjectExcel', $this->viewData);
    }
    /**
     * 车展基金报表-项目费用明细表
     * @param unknown $params
     */
    function getCarDetailExcel($params){
        //线上广宣传播明细
        $this->load->model('carProjectModel');
        $projectWhere = array('procId' => array('in', $this->viewData['procIds']));
        $projectList = $this->carProjectModel->getNewList($projectWhere, NULL, NULL, '', 'procId ASC');
        $this->viewData['projectList'] = $projectList;
        
        $this->load->view('Report/carDetailExcel', $this->viewData);
    }
    /**
     * 巡展基金报表初始化
     * @param number $reportType
     * @param array $params
     */
    function getXunExcel($reportType, $params){
        //主线流程条件
        $procWhere = array();
        //查找对应地区列表
        $placeStoreList = $this->sellpointModel->searchStore($params['area'], $params['region'], '', $params['province'], $params['city'], $params['storeId'], $params['keyword']);
        $placeStoreIds = array_keys($placeStoreList);
        $this->viewData['allStoreList'] = $placeStoreList;
        //权限内专营店列表
        $viewStoreIds = $this->userLib->getViewStoreIds();
        if ($viewStoreIds == '*'){
            $procWhere['storeId'] = array('in', $placeStoreIds);
        }else{
            $procWhere['storeId'] = array('in', array_intersect($viewStoreIds, $placeStoreIds));
        }
        $procWhere['year'] = $params['year'];
        $procWhere['timeUnit'] = $params['timeUnit'];
        $this->load->model('xunProcessModel');
        $procList = $this->xunProcessModel->getNewList($procWhere);
        $this->viewData['procList'] = $procList;
        //主线流程ID集
        $procIds = array_keys($procList);
        $this->viewData['procIds'] = $procIds;
        switch ($reportType){
            case 1:
                $this->getXunProjectExcel($params);
                break;
            case 2:
                $this->getXunDetailExcel($params);
                break;
        }
    }
    /**
     * 巡展基金报表-项目费用总表
     * @param array $params
     */
    function getXunProjectExcel($params){
        $this->load->view('Report/xunProjectExcel', $this->viewData);
    }
    /**
     * 巡展基金报表-项目费用明细表
     * @param unknown $params
     */
    function getXunDetailExcel($params){
        //线上广宣传播明细
        $this->load->model('xunProjectModel');
        $projectWhere = array('procId' => array('in', $this->viewData['procIds']));
        $projectList = $this->xunProjectModel->getNewList($projectWhere, NULL, NULL, '', 'procId ASC');
        $this->viewData['projectList'] = $projectList;
    
        $this->load->view('Report/xunDetailExcel', $this->viewData);
    }
    /**
     * 专项基金报表初始化
     * @param number $reportType
     * @param array $params
     */
    function getSpecialExcel($reportType, $params){
        //主线流程条件
        $procWhere = array();
        //查找对应地区列表
        $placeStoreList = $this->sellpointModel->searchStore($params['area'], $params['region'], '', $params['province'], $params['city'], $params['storeId'], $params['keyword']);
        $placeStoreIds = array_keys($placeStoreList);
        $this->viewData['allStoreList'] = $placeStoreList;
        //权限内专营店列表
        $viewStoreIds = $this->userLib->getViewStoreIds();
        if ($viewStoreIds == '*'){
            $procWhere['storeId'] = array('in', $placeStoreIds);
        }else{
            $procWhere['storeId'] = array('in', array_intersect($viewStoreIds, $placeStoreIds));
        }
        $procWhere['year'] = $params['year'];
        $procWhere['timeUnit'] = $params['timeUnit'];
        $this->load->model('specialProcessModel');
        $procList = $this->specialProcessModel->getNewList($procWhere);
        $this->viewData['procList'] = $procList;
        //主线流程ID集
        $procIds = array_keys($procList);
        $this->viewData['procIds'] = $procIds;
        switch ($reportType){
            case 1:
                $this->getSpecialProjectExcel($params);
                break;
            case 2:
                $this->getSpecialOnlineExcel($params);
                break;
            case 3:
                $this->getSpecialOfflineExcel($params);
                break;
        }
    }
    /**
     * 专营店营销基金-项目费用总表
     * @param array $params
     */
    function getSpecialProjectExcel($params){
        $this->load->view('Report/specialProjectExcel', $this->viewData);
    }
    /**
     * 专营店营销基金-线上广宣传播明细表
     * @param array $params
     */
    function getSpecialOnlineExcel($params){
        //线上广宣传播明细
        $this->load->model('specialOnlineModel');
        $onlineWhere = array('procId' => array('in', $this->viewData['procIds']));
        $onlineList = $this->specialOnlineModel->getNewList($onlineWhere, NULL, NULL, '', 'procId ASC');
        $this->viewData['onlineList'] = $onlineList;
    
        $this->load->view('Report/specialOnlineExcel', $this->viewData);
    }
    /**
     * 专营店营销基金-线下营销活动明细表
     * @param array $params
     */
    function getSpecialOfflineExcel($params){
        //线下营销活动明细
        $this->load->model('specialOfflineModel');
        $offlineWhere = array('procId' => array('in', $this->viewData['procIds']));
        $offlineList = $this->specialOfflineModel->getNewList($offlineWhere, NULL, NULL, '', 'procId ASC');
        $this->viewData['offlineList'] = $offlineList;
    
        $this->load->view('Report/specialOfflineExcel', $this->viewData);
    }
}